import mysql from 'mysql2';
import config from '../config/main.js';


function wrap(fn, ctx) {
    return (...args) => {
        const f = (done) => {
            args.push(done);
            fn.apply(ctx, args);
        };
        return f;
    };
}

const methods = [
    'query',
    'execute',
];

function wrapConnection(_conn) {
    const conn = _conn;
    if (conn.coWrapped) {
        return conn;
    }
    conn.coWrapped = true;
    methods.forEach((name) => {
        conn[name] = wrap(conn[name], conn);
    });
    return conn;
}

function createPool(opts) {
    const pool = mysql.createPool(opts);
    const getConn = pool.getConnection;
    const wrappedGetConnection = () => {
        const f = (done) => {
            getConn.call(pool, (err, conn) => {
                if (err) return done(err);
                return done(null, wrapConnection(conn));
            });
        };
        return f;
    };
    pool.getConnection = wrappedGetConnection;
    return pool;
}

const dbm = {};
const pool = createPool(config.mysql);

dbm.query = function* gen(sql, params) {
    const db = yield pool.getConnection();
    const rows = yield db.query(sql, params);
    db.release();
    return rows;
};

dbm.execute = function* exe(sql, params) {
    const db = yield pool.getConnection();
    const rows = yield db.execute(sql, params);
    db.release();
    return rows;
};

export default dbm;
